<script>
	export default {
		onLaunch: function() {
			console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
			// console.log('App Launch')
		},
		onShow: function() {
			// console.log('App Show')
			this.haveUpdate();
			uni.setTabBarItem({
			  index: 2,
				// #ifdef MP-WEIXIN
				iconPath: '/static/tabbar/Slice 7.png',
				selectedIconPath: '/static/tabbar/Slice 7.png'
				// #endif
				// #ifndef MP-WEIXIN
				iconPath: '/static/tabbar/yifugif.gif',
				selectedIconPath: '/static/tabbar/yifugif.gif'
				// #endif
			 
			})
		},
		onHide: function() {
			// console.log('App Hide')
		},
		methods: {
			haveUpdate() {
				// #ifdef  APP-PLUS
				// console.log("检查更新");
				uni.getSystemInfo({
					success: (params) => {
						// console.log(params, "系统")
						switch (params.osName) {
							case 'android':
								// // console.log('运行Android上')
								this.needUpdate('1');
								break;
							case 'ios':
								// // console.log('运行iOS上')
								this.needUpdate('2');
								break;
						}
					}
				})
				// #endif
				// #ifdef  H5
				// console.log("H5不需要检查更新");
				// #endif

			},
			needUpdate(platform) {
				let version = plus.runtime.version;
				this.$post({
					module: 'Utils',
					interface: 1002,
					data: {
						platform: platform,
						version: version,
					},
				}).then(res => {
					// console.log("版本更新", res)
					if (res.needUpgrade) {
						uni.showModal({
							title: "提示",
							content: "发现新版本，请点击确定更新。",
							showCancel: false,
							confirmText: "确定",
							confirmColor:'#FFF',
							cancelColor:'#F2615F',
							success: () => {
								this.download(res.newVersion.apkUrl);
							}
						})
					}
				})
			},
			download(url) {
				// console.log("下载");

				var dtask = plus.downloader.createDownload(url, {},
					function(d, status) {
						uni.showToast({
							title: '下载完成',
							mask: false,
							duration: 1000
						});
						// 下载完成
						// console.log('status: ' + status);
						if (status == 200) {
							// console.log('下载成功：' + d.filename);
							// console.log('plus.io.convertLocalFileSystemURL(d.filename): ' + plus.io.convertLocalFileSystemURL(d.filename))
							plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, function(success) {
								uni.showToast({
									title: '安装成功',
									mask: false,
									duration: 1500
								});
							}, function(error) {
								uni.showToast({
									title: '安装失败-01',
									mask: false,
									duration: 1500
								});
							})
						} else {
							uni.showToast({
								title: '更新失败-02',
								mask: false,
								duration: 1500
							});
						}
					});
				try {
					dtask.start(); // 开启下载的任务
					var prg = 0;
					var showLoading = plus.nativeUI.showWaiting("正在下载"); //创建一个showWaiting对象 

					dtask.addEventListener('statechanged', function(task, status) {
						// console.log('自动更新测试1：', task)
						// console.log('自动更新测试2：', status)
						// 给下载任务设置一个监听 并根据状态  做操作
						switch (task.state) {
							case 1:
								showLoading.setTitle("正在下载");
								break;
							case 2:
								showLoading.setTitle("已连接到服务器");
								break;
							case 3:
								prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) *
									100);
								showLoading.setTitle("  正在下载" + prg + "%  ");
								break;
							case 4:
								plus.nativeUI.closeWaiting();
								//下载完成
								break;
						}
					});
				} catch (err) {
					// console.log('错误信息', err)
					plus.nativeUI.closeWaiting();
				}

			},
		},
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	@import "uview-ui/index.scss";

	// 设置整个项目的背景色
	page {
		background-color: #ffffff;
	}

	view {
		box-sizing: border-box;
	}

	button {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	/* #endif */
	.example-info {
		font-size: 14rpx;
		color: #333;
		padding: 10rpx;
	}
	 .uni-swiper-dots{
		display: none;
	}
</style>
